package com.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dao.TestCaseDAO;
import com.entity.FieldType;
import com.entity.TestCase;
import com.entity.TestFun;

@Controller
public class dataGridController {
	@Autowired
	public TestCaseDAO testCaseDao;

	// TestFunctionality Data Grid

	@SuppressWarnings("unchecked")
	@RequestMapping("/TFgridDetails/{name}")
	public void dataGridTF(HttpServletRequest req,
			HttpServletResponse response, HttpSession session,
			@PathVariable String name) {
		System.out.println("inside TFgridDetails");
		response.setContentType("application/json");
		int index = 0;
		int size;
		System.out.println("TF name" + name);
		JSONArray j1 = new JSONArray();
		List<TestCase> lists = testCaseDao.listTestCaseByParent(name);
		System.out.println("TF list size :" + lists.size());
		size = lists.size();
		JSONObject out = new JSONObject();
		while (size != 0) {
			JSONObject j = new JSONObject();
			TestCase tc = lists.get(index);
			j.put("TestCaseNum", tc.getTestcaseNum());
			j.put("TestCaseName", tc.getTestCaseName());
			j.put("Description", tc.getDescription());
			j.put("Type", tc.getType());
			System.out.println("loop number" + index);
			index++;
			size--;
			j1.add(j);
		}
		out.put("items", j1);

		try {
			out.writeJSONString(response.getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("end of TFgridDetails");
	}

	// Test Cases Data Grid
	@SuppressWarnings("unchecked")
	@RequestMapping("/TCgridDetails/{tc}/{tf}")
	public void dataGridTC(HttpServletRequest req,
			HttpServletResponse response, HttpSession session,
			@PathVariable String tc, @PathVariable String tf) {
		System.out.println("inside TFgridDetails");
		response.setContentType("application/json");
		System.out.println(tc + " of " + tf);
		int index = 0;
		int size;
		JSONArray j1 = new JSONArray();
		List<FieldType> lists = testCaseDao.listFieldTypebyParentandTF(tc, tf);
		size = lists.size();
		System.out.println("Tc size list :" + size);
		JSONObject out = new JSONObject();
		while (size != 0) {
			JSONObject j = new JSONObject();
			FieldType ft = lists.get(index);
			j.put("FieldName", ft.getFieldName());
			j.put("Fieldtype", ft.getFieldType());
			j.put("Permission", ft.getPermision());
			j.put("FieldValue", ft.getFieldValue());
			j.put("Xpath", ft.getXpath());
			System.out.println("loop number" + index);
			index++;
			size--;
			j1.add(j);
			System.out.println(j);
			System.out.println(j1);
		}
		out.put("items", j1);

		try {
			out.writeJSONString(response.getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("end of TCgridDetails");

	}

	// Test Functionality Description
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/TFdescription/{name}")
	public void tfDescription(HttpServletRequest req,
			HttpServletResponse response, @PathVariable String name) {
		System.out.println("inside tfDescription");
		response.setContentType("application/json");
		System.out.println("TF name" + name);
		List<TestFun> fun = testCaseDao.listTestFunByName(name);
		TestFun tf = fun.get(0);
		System.out.println("Tf Details as follows");
		System.out.println(tf.getTfDescription() + "," + tf.getTfName() + ","
				+ tf.getTfStats());

		JSONObject j3 = new JSONObject();
		j3.put("id", "he");
		j3.put("name", tf.getTfName());
		JSONObject j31 = new JSONObject();
		j31.put("id", "he");
		j31.put("name", tf.getTfDescription());
		JSONObject j32 = new JSONObject();
		j32.put("id", "he");
		j32.put("name", tf.getTfStats());
		JSONObject j2 = new JSONObject();

		JSONArray j1 = new JSONArray();
		j1.add(j3);
		j1.add(j31);
		j1.add(j32);
		j2.put("books", j1);

		try {
			j2.writeJSONString(response.getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println("end of TfDescription");

	}

	// Test Case Description
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/TcDescription/{name}/{parent}")
	public void tcDescription(HttpServletRequest req,
			HttpServletResponse response, @PathVariable String name,
			@PathVariable String parent) {
		System.out.println("inside tcDescription");
		response.setContentType("application/json");
		System.out.println(name + " of " + parent);
		List<TestCase> fun = testCaseDao.listTestCaseByNameandParent(name,
				parent);
		TestCase tc = fun.get(0);
		System.out.println("Tc details as follows");
		System.out.println(tc.getDescription() + "," + tc.getTestCaseName());
		JSONObject j3 = new JSONObject();
		j3.put("id", "he");
		j3.put("name", tc.getTestCaseName());
		JSONObject j31 = new JSONObject();
		j31.put("id", "he");
		j31.put("name", tc.getDescription());
		JSONObject j2 = new JSONObject();
		JSONArray j1 = new JSONArray();
		j1.add(j3);
		j1.add(j31);
		j2.put("books", j1);
		try {
			j2.writeJSONString(response.getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println("end of tcDescription");
	}
}
